﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BPM.Base.Tools
{
    public static class MyConfig
    {
        //
        public static string getAppValue(string key)
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] != null)
            {
                return config.AppSettings.Settings[key].Value;
            }
            else
            {
                return null;
            }
        }

        //
        public static int? setAppValue(string key, string value)
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

            int saveTag = 0;
            if (config.AppSettings.Settings[key] != null)
            {
                config.AppSettings.Settings[key].Value = value;
                saveTag = 0;
            }
            else
            {
                config.AppSettings.Settings.Add(key, value);
                saveTag = 1;
            }


            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            return saveTag;

        }
        //
        public static int? setConnection(string conKey, string dataSource, string initialCatalog)
        {
            if (string.IsNullOrEmpty(conKey)) return null;
            if (string.IsNullOrEmpty(dataSource)) return null;
            if (string.IsNullOrEmpty(initialCatalog)) return null;

            int saveTag = 0;

            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

            string conncectionStr = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True", dataSource, initialCatalog);

            if (config.ConnectionStrings.ConnectionStrings[conKey] == null)
            {
                System.Configuration.ConnectionStringSettings csSettings = new System.Configuration.ConnectionStringSettings(conKey, conncectionStr, "System.Data.SqlClient");

                config.ConnectionStrings.ConnectionStrings.Add(csSettings);

                saveTag = 0;
            }
            else
            {
                config.ConnectionStrings.ConnectionStrings[conKey].ConnectionString = conncectionStr;

                saveTag = 1;
            }

            config.Save(System.Configuration.ConfigurationSaveMode.Modified);

            return saveTag;

        }

        //


        public static string getConnection(string conKey)
        {
            if (string.IsNullOrEmpty(conKey)) return null;

            System.Configuration.ConnectionStringSettings cons = System.Configuration.ConfigurationManager.ConnectionStrings[conKey];

            if (cons == null) return null;

            return cons.ConnectionString;

        }


    }
}
